package com.nsoft.thickclient.trackMove;
/**
 * 
 * @author venkat
 *
 */
public class StringHelper
{

   public static int countChar(String value, char c)
   {
      int count = 0;
      for (int i = 0; i < value.length(); i++)
         {
         if (value.charAt(i) == c)
            {
            count++;
         }
      }
      return count;
   }

   public static int[] indexesOf(String value, char c)
   {
      int[] indexes = new int[value.length()];
      int[] indexesResult = new int[0];
      int startIndex = 0;
      int index = 0;
      int i = 0;

      while ((index = value.indexOf(c, startIndex)) > 0)
         {
         indexes[i] = index;
         startIndex = index + 1;
         i++;
      }
      indexesResult = new int[i];

      if (i > 0)
         {
         System.arraycopy(indexes, 0, indexesResult, 0, i);
      }

      return indexesResult;
   }

   public static String fillEmpty(int i)
   {
      String res = "";
      while (i > 0)
         {
         res += " ";
         i--;
      }
      return res;
   }

   /**
    * Remove the specified character from thre string
    */
   public static String removeChar(char c, String value)
   {
      StringBuffer result = new StringBuffer(value);
      int i = result.toString().indexOf(c);

      while (i >= 0)
         {
         result.deleteCharAt(i);
         i = result.toString().indexOf(c);
      }
      return result.toString();
   }

   public static String replace(String find, String replace, String source)
   {
      String result = "";

      while (source.indexOf(find) >= 0)
         {
         result += source.substring(0, source.indexOf(find)) + replace;
         source = source.substring(source.indexOf(find) + find.length());
      }
      result += source;

      return result;
   }

   /**
   * Find the last occurrence of "find" in the "source" String
   * and replace it by "replace".
   * @param find - String to find
   * @param replace - String to replace
   * @param source - the source String to process.
   * @return - the replace result.
   */
   public static String replaceLast(String find, String replace, String source)
   {
      String result = "";

      if (source.indexOf(find) >= 0)
         {
         result += source.substring(0, source.lastIndexOf(find)) + replace;
         source = source.substring(source.lastIndexOf(find) + find.length());
      }
      result += source;

      return result;
   }

   /** 
   * This method truncs the string s with the maximum length of
   * maxLength caracters
   * @param source - the String.
   * @param maxLength - the length.
   * @return The trunced String
   */
   public static String truncate(String source, int maxLength)
   {
      if (source == null)
         {
         return null;
      }
      if (source.length() <= maxLength)
         {
         return source;
      }
      return source.substring(0, maxLength);
   }

   /**
   * Replicate the value "count" times.
   * @param value - value to replicate.
   * @param count - number of replications.
   * @return - the replicated value.
   */
   static public String replic(String value, int count)
   {
      String result = "";

      // Replicate
      for (int i = 0; i < count; i++)
         {
         result += value;
      }
      return result;
   }
}